package com.keke.service.user;

import com.keke.ado.baseDao;
import com.keke.ado.user.UserDao;
import com.keke.ado.user.UserDaoImpl;
import com.keke.pojo.User;
import com.keke.util.PageSupport;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {

    //业务层都会调用Dao层，所以我们要引入Dao层
    private UserDao userDao;
    public UserServiceImpl(){
        userDao=new UserDaoImpl();
    }

    public User login(String userCode, String password) {
        Connection connection=null;
        User user=null;
        try {
            connection = baseDao.getConnection();
            System.out.println("myconn"+connection);
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection, userCode, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }

        return user;
    }

    public boolean updatePwd(int id, String pwd) {
        Connection connection=null;
        boolean flag=false;
        //修改密码
        try {
            connection=baseDao.getConnection();
            if(userDao.updatePwd(connection,id,pwd)>0){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }

        return flag;
    }

    //查询记录数
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int count=0;
        try {
            connection=baseDao.getConnection();
            count = userDao.getUserCount(connection, username, userRole);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return count;
    }

    //得到用户列表
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection=null;
        List<User> userList=null;
        System.out.println("queryUserName ---> "+queryUserName);
        System.out.println("queryUserRole ---> "+queryUserRole);
        System.out.println("currentPageNo ---> "+currentPageNo);
        System.out.println("pageSize ---> "+pageSize);

        try {
            connection=baseDao.getConnection();
            userList=userDao.getUserList(connection,queryUserName,queryUserRole,currentPageNo,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return userList;
    }

    //添加用户
    public boolean add(User user) {
        boolean flag=false;
        Connection connection=null;
        try {
            connection=baseDao.getConnection();
            connection.setAutoCommit(false);
            int updateRows=userDao.add(connection,user);
            connection.commit();
            if(updateRows>0){
                flag=true;
                System.out.println("add success!!");
            }
            else {
                System.out.println("add failed!!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                System.out.println("=========rollback!!!!=========");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            baseDao.close(connection,null,null);
        }
        return flag;
    }

    //删除用户
    public boolean deleteUserById(Integer delId) {
        Connection connection=null;
        boolean flag= false;
        try{
            connection=baseDao.getConnection();
            if(userDao.deleteUserById(connection,delId)>0){
                flag=true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return flag;
    }

    //利用id查询user
    public User getUserById(String id) {
        User user = null;
        Connection connection = null;
        try{
            connection = baseDao.getConnection();
            user = userDao.getUserById(connection,id);
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            baseDao.close(connection,null,null);
        }
        return user;
    }

    //修改用户
    public int modify(User user){
        Connection connection = null;
        int updateRows = 0;
        try {
            connection = baseDao.getConnection();
            updateRows = userDao.modify(connection, user);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baseDao.close(connection, null, null);
        }
        return updateRows;
    }

    //获取编码是否存在
    public User getUserByUserCode(String userCode) {
        Connection connection = null;
        User user = null;
        try {
            connection = baseDao.getConnection();
            user = userDao.getUserByUserCode(connection, userCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baseDao.close(connection, null, null);
        }
        return user;
    }
}
